/******************************************************下拉框*****************************************************/

var onComboTreeClick = function(event, treeId, treeNode) {

    var panel = $(event.target).closest("div.combo_panel");
    if(panel.size()>0){
        var comboId = panel.attr("comboId");
        var combo = _$.getById(comboId);
        if(treeNode){
            var treeObj = $.fn.zTree.getZTreeObj(treeId);

            var parentKey = combo.getOption("parentKey");
            var idField = combo.getOption("idField");
            var textField = combo.getOption("textField");
            var multiple = combo.getOption("multiple");

           if(!multiple) {
               var code = treeNode[idField];
               combo.setValue(code);
               hideComboPanel(comboId);
           }
        }
    }
};
var onComboTreeCheck = function(event, treeId, treeNode) {

    var panel = $(event.target).closest("div.combo_panel");
    if(panel.size()>0){
        var comboId = panel.attr("comboId");
        var combo = _$.getById(comboId);
        if(treeNode){
            var treeObj = $.fn.zTree.getZTreeObj(treeId);

            var parentKey = combo.getOption("parentKey");
            var idField = combo.getOption("idField");
            var textField = combo.getOption("textField");
            var multiple = combo.getOption("multiple");

            if(multiple) {
                var code = treeNode[idField];
                var checked = treeNode.checked;
                var value = combo.value;
                var values = [];
                if(isNotEmpty(value)){
                    values = (value+"").split(",");
                }
                if(checked){
                    if(values.indexOf(code) == -1){
                        values.add(code);
                        combo.setValue(values.join(","));
                    }
                }else{
                    if(values.indexOf(code) != -1){
                        values.remove(code);
                        combo.setValue(values.join(","));
                    }
                }

            }
        }
    }
};

_$.ComboTree=function (target){
    var _this = this;
    _$.ComboTree.superclass.constructor.call(_this,target);
};
_$.regClass("ComboTree",_$.ComboTree);

_$.extend(_$.ComboTree,_$.Combo, {
    _cls:_$._clsPre + "ComboTree",
    _uiCls:_$._uiPreCls + "ComboTree",
    _attrProps:function(){
        var _this = this;
        var properties = _$.ComboTree.superclass._attrProps.call(_this);
        properties.addAll(["basePath","parentKey","dictCode","apiServer","call"]);
        return properties;
    },
    _attrOpts:function(){
        var _this = this;
        var opts = _$.ComboTree.superclass._attrOpts.call(_this);
        return $.extend(true,opts,{
            'dictCode':'',
            "idField":"id",
            "textField":"name",
            "parentKey":"parentId",
            "leaf":"leaf",
            "level":"level",
            "namePath":"namePath",
            "basePath":"/base",
            "apiServer":"base",
            "call":"dict.selectNodes"
        });
    },
    _init:function(){
        var _this = this;
        if(!_this._isEdit()){
            _this._initShow();
        }
    },
    _initShow:function(){
        var _this = this;
        _this._loadData(_this._showInit);
    },

    _initPanel:function(){
        var _this = this;
        _this._initData();
    },
    _initData:function(){
        var _this = this;
        _this._loadData(_this._editInit);
        _this._bindPanelEvents();
    },
    _loadData:function(func){
        var _this = this;

        var parentKey = _this.getOption("parentKey");
        var dictCode = _this.getOption("dictCode");
        var apiServer = _this.getOption("apiServer");
        var call = _this.getOption("call");
        var idField = _this.getOption("idField");
        var basePath = _this.getOption("basePath");

        var params = {
            dictCode:dictCode
        };

        if(_this._isEdit()){
            $(".combobox_item",_this.panel_content).unbind();
            _this.panel_content.empty();
        }
        _this.data = [];

        var route = {
            "apiServer":apiServer,
            "call":call,
            "description":"查询树形"
        };

        var url=basePath + "/callService";
        var options = {
            url:url,
            route:route,
            data:params,
            callback:function(result){
                func.call(_this,result);
            }
        } ;
        $.cc.ajaxDtoRequestCommon(options);
    },
    _editInit:function(result){
        var _this = this;
        var state = result.getState();
        var info = result.getInfo();
        if(state == 0){
            var list = result.get("list");
            _this.data = list;
            _this._setData(list);
            var initValue= _this.options["initValue"];
            _this.setValue(initValue);
        }else{
            $.cc.error(info);
        }
    },
    _setData:function(list){
        var _this = this;
        var parentKey = _this.getOption("parentKey");
        var idField = _this.getOption("idField");
        var textField = _this.getOption("textField");
        var basePath = _this.getOption("basePath");
        var multiple = _this.getOption("multiple");

       if(list != null && list.length>0){

           $(".combobox_item",_this.panel_content).unbind();
           _this.panel_content.empty();

           var _html = '<div id="tree_'+_this.id+'" class="ztree"></div>';
           _this.panel_content.append(_html);

           if(!multiple){
               var setting = {
                   data: {
                       simpleData: {
                           enable: true,
                           idKey: idField,
                           pIdKey: parentKey,
                           name:textField
                       }
                   },
                   callback: {
                       onClick: onComboTreeClick
                   },
                   view: {
                       showLine:false
                   }
               };
               $.fn.zTree.init($("#tree_"+_this.id), setting, list);
           }else{
               var setting = {
                   data: {
                       simpleData: {
                           enable: true,
                           idKey: idField,
                           pIdKey: parentKey,
                           name:textField
                       }
                   },
                   callback: {
                       onCheck: onComboTreeCheck
                   },
                   check: {
                       enable: true,
                       chkboxType: { "Y": "", "N": "" }
                   },
                   view: {
                       showLine:false
                   }
               };
               $.fn.zTree.init($("#tree_"+_this.id), setting, list);
           }
       }
        _this._loadSuccess();
    },
    _showInit:function(result){
        var _this = this;
        var state = result.getState();
        var info = result.getInfo();
        if(state == 0){
            var list = result.get("list");
            _this.data = list;

            var initValue= _this.options["initValue"];
            var idField= _this.options["idField"];
            var textField= _this.options["textField"];

            if(initValue != null){
                var values = (initValue+"").split(",");
                if(values.length>0) {
                    var names = [];
                    for (var i = 0; i < values.length; i++) {
                        var itemValue = values[i];

                        list.each(function (item, i) {
                            var id = item[idField];
                            var name = item[textField];
                            if (id == itemValue) {
                                names.add(name);
                            }
                        });
                    }
                    if (names.length > 0) {
                        _this.value = values.join(",");

                        if (_this._isEdit()) {
                            _this.combo_input.html(names.join(","));
                        } else {
                            _this.component.html(names.join(","));
                        }
                    } else {
                        _this.value = null;

                        if (_this._isEdit()) {
                            _this.combo_input.html('');
                        } else {
                            _this.component.html('');
                        }
                    }
                }
            }


        }else{
            $.cc.error(info);
        }
        _this._loadSuccess();
    },
    getValue:function(){
        var _this = this;
        return _this.value;
    },
    setValue:function(value,change){
        var _this = this;
        if(isEmpty(change)){
            change = true;
        }


        if(isEmpty(value) || isEmpty(_this.data) || _this.data.length == 0){
            _this.value = null;
            if (_this._isEdit()) {
                _this.combo_input.html("");
            } else {
                _this.component.html("");
            }
        }else{
            var multiple = _this.getOption("multiple");
            var idField = _this.getOption("idField");
            var textField = _this.getOption("textField");

            if(!multiple){

                var flag = _this.data.some(function(item,i){
                    var id = item[idField];
                    var name = item[textField];

                    if(id == value){
                        _this.value = id;

                        if (_this._isEdit()) {
                            _this.combo_input.html(name);
                        } else {
                            _this.component.html(name);
                        }
                        return true;
                    }
                    return false;
                });

                if(!flag){
                    _this.value = null;
                    if (_this._isEdit()) {
                        _this.combo_input.html("");
                    } else {
                        _this.component.html("");
                    }
                }
            }else{
                var values = (value+"").split(",");
                if(values.length>0){
                    var names=[] ;
                    for(var i=0;i<values.length;i++){
                        var itemValue = values[i];

                        _this.data.each(function(item,i){
                            var id = item[idField];
                            var name = item[textField];
                            if(id == itemValue){
                                names.add(name);
                            }
                        });
                    }
                    if(names.length>0){
                        _this.value = values.join(",");

                        if (_this._isEdit()) {
                            _this.combo_input.html(names.join(","));
                        } else {
                            _this.component.html(names.join(","));
                        }
                    }else{
                        _this.value = null;

                        if (_this._isEdit()) {
                            _this.combo_input.html('');
                        } else {
                            _this.component.html('');
                        }
                    }

                    var treeObj = $.fn.zTree.getZTreeObj("tree_" + _this.id);
                    treeObj.checkAllNodes(false);

                    values.each(function(val){
                        var node = treeObj.getNodeByParam(idField,val, null);

                        treeObj.checkNode(node, true, false);
                    });
                }
            }
        }

        if(change){
            _this._fire("onchange");
        }
    },
    _hideMore:function(){
        var _this = this;
        var multiple = _this.getOption("multiple");
        if(multiple){
            _this.show_more_bar.addClass("icon_right").removeClass("icon_left");
            _this.show_more.empty();
            _this.show_more.hide();

        }
    },
    _showMore:function(){
        var _this = this;
        var multiple = _this.getOption("multiple");
        if(multiple){
            hideComboPanel(_this.id);
            _this.show_more_bar.addClass("icon_left").removeClass("icon_right");
            _this.show_more.empty();
            _this.show_more.show();

            if(isNotEmpty(_this.value)){
                var values = (_this.value+"").split(",");
                var names = [];

                if(values.length>0){
                    for(var i=0;i<values.length;i++){
                        var id = values[i];
                        var name = names[i];
                        var _html='<div class="data_item" ><span>'+name+'</span><i class="deleteicon" data-value="'+id+'"></i></div>';
                        _this.show_more.append(_html);
                    }

                    $(".data_item .deleteicon",_this.show_more).click(function(){
                        var val = $(this).attr("data-value");

                        if(values.indexOf(val) != -1){
                            values.remove(val);
                            _this.setValue(values.join(","));
                            var item = $(this).closest(".data_item");
                            $(this).unbind();
                            item.remove();
                        }
                        return false;
                    });
                }
            }
        }
    }
});
